
package com.jf.cloud.service.sysmanager.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jf.cloud.service.sysmanager.common.base.BaseService;
import com.jf.cloud.service.sysmanager.common.base.GenericsUtils;
import com.jf.cloud.service.sysmanager.common.base.OPaging;
import com.jf.cloud.service.sysmanager.common.db.BaseDao;
import com.jf.cloud.service.sysmanager.common.db.Condition;
import com.jf.cloud.service.sysmanager.common.db.Query;
import com.jf.cloud.service.sysmanager.common.taglib.Paging;
import com.jf.cloud.service.sysmanager.common.util.StringUtil;
import com.jf.cloud.service.sysmanager.dao.IPubgExchangeGoodsService;
import com.jf.cloud.service.sysmanager.entity.PubgExchangeGoods;


/**
 * 商城物品服务实现
 * 
 * @author 绝峰代码机1.0 创建时间：2018-03-31
 */
@Service("pubgExchangeGoodsService")
public class PubgExchangeGoodsServiceImpl extends BaseService<PubgExchangeGoods, Long> implements IPubgExchangeGoodsService {

	/**
	 * 日志
	 */
	private static Log log = LogFactory.getLog(PubgExchangeGoodsServiceImpl.class);

	/**
	 * JPA公共DAO
	 */
	@Resource
	private BaseDao baseDao;

	private RowMapper<PubgExchangeGoods> rowMapper = new BeanPropertyRowMapper<PubgExchangeGoods>(GenericsUtils.getSuperClassGenricType(this.getClass()));

	/**
	 * 根据主键查询商城物品
	 * 
	 * @author 绝峰代码机1.0 创建时间：2018-03-31
	 */
	public PubgExchangeGoods findPubgExchangeGoods(Long rowId) throws Exception {
		try {
			return this.baseDao.findObject("", rowMapper, rowId);
		} catch (Exception e) {
			throw new Exception("根据主键查询商城物品出错！", e);
		}
	}

	/**
	 * 根据查询条件获取商城物品列表
	 * 
	 * @author 绝峰代码机1.0 创建时间：2018-03-31
	 */
	public List<PubgExchangeGoods> findPubgExchangeGoodsList(Condition qc) throws Exception {
		try {
			return this.baseDao.findList("", rowMapper);
		} catch (Exception e) {
			throw new Exception("根据查询条件获取商城物品列表出错！", e);
		}
	}

	/**
	 * 新增/修改商城物品
	 * 
	 * @author 绝峰代码机1.0 创建时间：2018-03-31
	 */
	@Transactional
	public PubgExchangeGoods saveOrUpdatePubgExchangeGoods(PubgExchangeGoods pubgExchangeGoods) throws Exception {
		try {
			return this.baseDao.saveAll(pubgExchangeGoods);
		} catch (Exception e) {
			throw new Exception("新增/修改商城物品出错！", e);
		}
	}

	/**
	 * 更新商城物品信息
	 *
	 * @author 代码机1.0 创建时间：2018-03-31
	 */
	@Transactional
	public void updatePubgExchangeGoods(PubgExchangeGoods pubgExchangeGoods) throws Exception {
		try {
			if (pubgExchangeGoods != null && StringUtil.isNotEmpty(pubgExchangeGoods.getGoodsName())) {
				Query query = new Query();
				query.put("_NOT_EQUAL$ROW_ID", pubgExchangeGoods.getRowId());
				query.put("GOODS_NAME", pubgExchangeGoods.getGoodsName());
				List<PubgExchangeGoods> goodsList = findList(query);
				if (goodsList == null || goodsList.size() == 0) {
					StringBuilder updateSql = new StringBuilder("UPDATE pubg_exchange_goods SET OPT_ID = ?, OPT_ACCOUNT = ?, OPT_TIME = ? ");
					List<Object> params = new ArrayList<Object>();
					params.add(pubgExchangeGoods.getOptId());
					params.add(pubgExchangeGoods.getOptAccount());
					params.add(pubgExchangeGoods.getOptTime());
					if (pubgExchangeGoods.getGoodsTypeId() != null && pubgExchangeGoods.getGoodsTypeId() > 0) {
						updateSql.append(", GOODS_TYPE_ID = ?");
						params.add(pubgExchangeGoods.getGoodsTypeId());
					}
					if (StringUtil.isNotEmpty(pubgExchangeGoods.getGoodsTypeName())) {
						updateSql.append(", GOODS_TYPE_NAME = ?");
						params.add(pubgExchangeGoods.getGoodsTypeName());
					}
					if (StringUtil.isNotEmpty(pubgExchangeGoods.getGoodsName())) {
						updateSql.append(", GOODS_NAME = ?");
						params.add(pubgExchangeGoods.getGoodsName());
					}
					if (pubgExchangeGoods.getGoodsPrice() != null && pubgExchangeGoods.getGoodsPrice() > 0) {
						updateSql.append(", GOODS_PRICE = ?");
						params.add(pubgExchangeGoods.getGoodsPrice());
					}
					if (pubgExchangeGoods.getOpenFlag() != null) {
						updateSql.append(", OPEN_FLAG = ?");
						params.add(pubgExchangeGoods.getOpenFlag());
					}
					updateSql.append(" WHERE ROW_ID=? ");
					params.add(pubgExchangeGoods.getRowId());
					baseDao.update(updateSql.toString(), params.toArray());
				}
			}
		} catch (Exception e) {
			throw new RuntimeException("更新商城物品出错！", e);
		}
	}
	
	/**
	 * app获取兑换商品列表
	 * 
	 * @author 朱建文 创建时间：2018年4月11日 下午1:58:07
	 */
	public List<Map<String, Object>> getExchageGoodsList(OPaging<Map<String, Object>> page) throws Exception {
		List<Map<String, Object>> dataList = new ArrayList<Map<String,Object>>();
		String sql = "SELECT ROW_ID rowId, GOODS_NAME goodsName, GOODS_PRICE goodsPrice ";
		sql += " FROM pubg_exchange_goods WHERE OPEN_FLAG = 1";
        Paging<Map<String, Object>> pageTemp = this.baseDao.findObjectListMapByPage(sql, page);
		dataList = pageTemp.getDataList();
		return dataList;
	}
}
